package com.peach.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peach.mapper.NewsMapper;
import com.peach.pojo.News;
import com.peach.pojo.NewsType;
import com.peach.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @GetMapping("/selectAllNews")
    public String selectAllNewsByConditions(Model model,
                                Integer newsTypeId,
                                String newsTitle,
                                String newsAuthor,
                                @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                @RequestParam(defaultValue="5",value="pageSize")Integer pageSize
    ){

        //为了程序的严谨性，判断非空：
        if(pageNum == null){
            //设置默认当前页
            pageNum = 1;
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            //设置默认每页显示的数据数
            pageSize = 5;
        }

        PageHelper.startPage(pageNum,pageSize);

        try {
            List<News> news = newsService.selectAllNews(newsTypeId,newsTitle,newsAuthor);

            List<NewsType> newsTypes = newsService.selectAllNewsTypes();
            model.addAttribute("newsTypes",newsTypes);
            PageInfo<News> pageInfo = new PageInfo<News>(news,pageSize);
            model.addAttribute("pageInfo",pageInfo);

            model.addAttribute("newsTypeId",newsTypeId);
            model.addAttribute("newsTitle",newsTitle);
            model.addAttribute("newsAuthor",newsAuthor);

        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "/news/news_list";
    }

    @GetMapping("/updateNews")
    public ModelAndView selectOneNews(Integer newsId,ModelAndView mv){
        System.out.println("??");
        System.out.println(newsId);
        News news = newsService.selectOneNews(newsId);
        System.out.println(news);
        mv.addObject("news",news);
        mv.setViewName("/news/news_update");
        List<NewsType> newsTypes = newsService.selectAllNewsTypes();
        mv.addObject("newsTypes",newsTypes);
        return mv;
    }
    @PostMapping("/toUpdate")
    public String toUpdate(News news,ModelAndView mv){
        newsService.updateOneNews(news);
        return "redirect:/news/selectAllNews";
    }

    @GetMapping("/deleteOneNews")
    public String deleteOneNews(Integer newsId){
        newsService.deleteOneNews(newsId);
        return "redirect:/news/selectAllNews";
    }

    @GetMapping("/addOneNews")
    public String addOneNews(Model model){
        List<NewsType> newsTypes = newsService.selectAllNewsTypes();
        model.addAttribute("newsTypes",newsTypes);
        return "/news/news_add";
    }

    @PostMapping("/toAddNews")
    public String toAddNews(News news){
        newsService.insertOneNews(news);
        return "redirect:/news/selectAllNews";
    }
}
